package org.dromara.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.bo.LocationGgBo;
import org.dromara.business.domain.vo.LocationGgVo;
import org.dromara.business.service.LocationGgService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * CRUD地理位置
 *
 * @author Lion Li
 * @date 2025-01-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/gg")
public class LocationGgController extends BaseController {

    private final LocationGgService locationGgService;

    /**
     * 查询CRUD地理位置列表
     */
    @SaCheckPermission("business:gg:list")
    @GetMapping("/list")
    public TableDataInfo<LocationGgVo> list(LocationGgBo bo, PageQuery pageQuery) {
        return locationGgService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出CRUD地理位置列表
     */
    @SaCheckPermission("business:gg:export")
    @Log(title = "CRUD地理位置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(LocationGgBo bo, HttpServletResponse response) {
        List<LocationGgVo> list = locationGgService.queryList(bo);
        ExcelUtil.exportExcel(list, "CRUD地理位置", LocationGgVo.class, response);
    }

    /**
     * 获取CRUD地理位置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:gg:query")
    @GetMapping("/{id}")
    public R<LocationGgVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(locationGgService.queryById(id));
    }

    /**
     * 新增CRUD地理位置
     */
//    @SaCheckPermission("business:gg:add")  权限
    @Log(title = "CRUD地理位置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LocationGgBo bo) {
        return toAjax(locationGgService.insertByBo(bo));
    }

    /**
     * 修改CRUD地理位置
     */
    @SaCheckPermission("business:gg:edit")
    @Log(title = "CRUD地理位置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LocationGgBo bo) {
        return toAjax(locationGgService.updateByBo(bo));
    }

    /**
     * 删除CRUD地理位置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:gg:remove")
    @Log(title = "CRUD地理位置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(locationGgService.deleteWithValidByIds(List.of(ids), true));
    }
}
